package com.raycloud.erp.omll.provider.service.impl;

import cn.hutool.http.Method;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.omall.open.sdk.domain.LogisticsCompany;
import com.omall.open.sdk.domain.ShipDetail;
import com.omall.open.sdk.response.BatchTagShippedResponse;
import com.raycloud.erp.mulplatforms.domain.ConsignInfo;
import com.raycloud.erp.mulplatforms.domain.ConsignUploadReqDto;
import com.raycloud.erp.mulplatforms.domain.ErpTrade;
import com.raycloud.erp.mulplatforms.domain.TradeConsignReqDto;
import com.raycloud.erp.omll.data.entity.MulPlatformUser;
import com.raycloud.erp.omll.data.entity.Order;
import com.raycloud.erp.omll.data.mapper.OrderMapper;
import com.raycloud.erp.omll.data.service.MulPlatformUserService;
import com.raycloud.erp.omll.provider.constant.Constants;
import com.raycloud.erp.omll.provider.model.bo.BatchTagShippedBO;
import com.raycloud.erp.omll.provider.service.BatchTagShippedService;
import com.raycloud.erp.omll.provider.utils.OmallHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 订单发货
 *
 * @author 枫伦
 * @DESCRIPTION
 * @create 2021/1/6 11:09 上午
 */
@Service
@Slf4j
public class BatchTagShippedServiceImpl implements BatchTagShippedService {
    @Autowired
    private MulPlatformUserService mulPlatformUserService;
    @Autowired
    private OmallHttpUtils omallHttpUtils;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LogisticsNoService logisticsNoService;

    /***
     * @DESCRIPTION: 发货
     * @author 枫伦
     * @params: [tradeConsignReqDto]
     * @return: java.lang.Boolean
     * @Date: 2021/1/14 7:45 下午
     * @Modified By:
     * @param tradeConsignReqDto
     */
    @Override
    public Boolean uploadTradeConsign(TradeConsignReqDto tradeConsignReqDto) {
        MulPlatformUser mulPlatformUser = new MulPlatformUser();
        mulPlatformUser.setUserId(tradeConsignReqDto.getUserId());
        mulPlatformUser.setCompanyId(tradeConsignReqDto.getCompanyId());
        MulPlatformUser userByUserIdCompanyId = mulPlatformUserService.getUserByUserIdCompanyId(mulPlatformUser);
        if (Objects.isNull(userByUserIdCompanyId)) {
            return false;
        }
        Map headMap = omallHttpUtils.getHeadMap(userByUserIdCompanyId);
        for (ConsignUploadReqDto consignUploadReqDto : tradeConsignReqDto.getConsignUploadReqDtoList()) {
            //交易信息
            ErpTrade erpTrade = consignUploadReqDto.getTrade();
            //物流信息
            ConsignInfo consignInfo = consignUploadReqDto.getConsignInfo();
            LogisticsCompany logisticsCompany = logisticsNoService.queryCompany(consignInfo.getExpressCompany().getName());
            if (null == logisticsCompany) {
                log.info("[洋葱]暂不支持物流公司[" + consignInfo.getExpressCompany().getName() + "]发货");
            }
            BatchTagShippedBO batchTagShippedBO = new BatchTagShippedBO();
            //查询子货号
            QueryWrapper<Order> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userByUserIdCompanyId.getUserId());
            queryWrapper.eq("company_id", userByUserIdCompanyId.getCompanyId());
            queryWrapper.eq("tid", erpTrade.getTid());
            List<Order> orderList = orderMapper.selectList(queryWrapper);
            List<ShipDetail> detail = new ArrayList<>();
            for (Order order : orderList) {
                ShipDetail shipDetail = new ShipDetail();
                //子货号
                shipDetail.setSkuCode(order.getSkuId());
                //物流公司简码
                shipDetail.setLogisticsNo(consignInfo.getExpressCompany().getCode());
                //物流公司名称
                shipDetail.setLogisticsCompany(consignInfo.getExpressCompany().getName());
                detail.add(shipDetail);
            }
            //平台订单号
            batchTagShippedBO.setPlatformCode(erpTrade.getTid());
            //物流信息
            batchTagShippedBO.setDetail(detail);
            BatchTagShippedResponse response = (BatchTagShippedResponse) omallHttpUtils.requestWithParams(Method.POST, Constants.RequestUrl.RETURN_ORDER_INDEX, headMap, BatchTagShippedResponse.class, omallHttpUtils.Obj2Map(batchTagShippedBO));
        }
        return true;
    }
}
